/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
import model.*;
import entities.*;
import java.math.BigDecimal;
import java.util.*;

/**
 *
 * @author xuanthuy
 */
@Named(value = "serviceManagedBean")
@SessionScoped
public class ServiceManagedBean implements Serializable {

    @EJB
    private DomainFacade domainFacade;

    @EJB
    private ServiceFacade serviceFacade;

    private Service service = new Service();

    private int Domain_ID;

    public Service getService() {
        return service;
    }

    public int getDomain_ID() {
        return Domain_ID;
    }

    public void setDomain_ID(int Domain_ID) {
        this.Domain_ID = Domain_ID;
    }

    public void setService(Service service) {
        this.service = service;
    }

    private String message = "";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ServiceManagedBean() {
    }

    public List<Service> findAll() {
        return this.serviceFacade.findAll();
    }

    public String edit(Service service) {
        this.service = service;
        return "serviceAdEdit";
    }

    public String xulycapnhat() {
        this.serviceFacade.edit(this.service);
        return "serviceAd?faces-redirect=true";
    }

    public void xoa(Service service) {
        this.serviceFacade.remove(service);
    }

    public void xoaAn(Service service) {
        this.service = service;

        this.service.setStatus(Boolean.FALSE);
        this.serviceFacade.edit(this.service);
    }

    public List<Service> sudungtruyvan() {
        return this.serviceFacade.truyvanAll();
    }

    public List<Domain> findDomains() {
        return this.domainFacade.findAll();
    }

    public String recoverDatadelete() {
        return "serviceAdRecover";
    }

    // liet ke status = false
    public List<Service> sudungtruyvan1() {
        return this.serviceFacade.truyvan1();
    }

    public String recoverData(Service service) {
        this.service = service;

        this.service.setStatus(Boolean.TRUE);
        this.serviceFacade.edit(this.service);
        return "serviceAd?faces-redirect=true";
    }

    public String add() {
        return "serviceAdAdd?faces-redirect=true";
    }

    public String addService() {
        this.service.setStatus(Boolean.TRUE);
        this.serviceFacade.create(this.service);
        return "serviceAd?faces-redirect=true";
    }

    public void clear() {
        this.service.setName("");
        this.service.setDescriptions("");
        this.service.setDetailDescription("");
        this.service.setPrice(BigDecimal.ZERO);
    }

    public String checkname() {
        Service temp = this.serviceFacade.checkName(service.getName());
        if (temp == null) {
            this.service.setStatus(Boolean.TRUE);
            this.serviceFacade.create(service);
            //this.message = "";
            return "serviceAd?faces-redirect=true";
        } else {
            this.message = service.getName()+" already exists! Please choose Name different!";
            return "";
        }
    }
}
